﻿@using Discussion.Core.Mvc
@using Discussion.Web.Services.UserManagement.Avatar
@inject IAvatarUrlService AvatarUrlService
@inject Discussion.Core.Models.SiteSettings SiteSettings
@model Discussion.Core.Pagination.Paged<Discussion.Core.Models.Topic>

@{
    ViewData["Title"] = "全部话题";

    var prevDisabled = Model.Paging.HasPreviousPage ? "" : "disabled";
    var nextDisabled = Model.Paging.HasNextPage ? "" : "disabled";

    var user = Context.DiscussionUser();
    var siteCanCreateTopic = SiteSettings.CanCreateNewTopics();
    var userCanCreateTopic = user != null && (!SiteSettings.RequireUserPhoneNumberVerified || user.PhoneNumberId.HasValue);
    var canCreateTopic = siteCanCreateTopic && userCanCreateTopic;
}



<div class="row">
    <div class="panel no-padding panel-topic-list col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div class="panel-heading">
            全部话题
            @if(canCreateTopic)
            {
                <span class="pull-right">
                    @Html.ActionLink("创建新话题", "Create", "Topic")
                </span>
            }
        </div>

        <div class="panel-body no-padding">
            @foreach (var topicItem in Model.Items)
            {
                <div class="topic-item">
                    <img src="@AvatarUrlService.GetAvatarUrl(topicItem.CreatedByUser)" title="话题作者 @topicItem.CreatedByUser.DisplayName" class="topic-author-avatar"/>
                    <div class="topic-meta-primary pull-left">
                        <span class="topic-type">@Html.DescribeTopicType(topicItem.Type)</span>
                    </div>
                    <a class="topic-title" href="@Url.Action("Index", "Topic", new {id = topicItem.Id})" title="@topicItem.Title">
                        @topicItem.Title
                    </a>
                    @if (topicItem.LastRepliedAt.HasValue)
                    {
                        <div class="topic-meta-secondary pull-right hidden-xs">
                            @if (topicItem.LastRepliedAuthor != null)
                            {
                                <img src="@AvatarUrlService.GetAvatarUrl(topicItem.LastRepliedAuthor)" title="最后回复来自 @topicItem.LastRepliedAuthor.DisplayName"  class="last-replied-user-avatar"/>
                            }
                            <span class="last-replied-at" data-last-replied-at="@Html.Timestamp(topicItem.LastRepliedAt)"></span>
                        </div>
                    }
                </div>
            }
            
            @if (Model.Items.Length < 1)
            {
                <div class="no-topics-notice">
                    没有主题
                </div>
            }
        </div>
        <div class="panel-footer">
            @if (Model.Items.Length > 0)
            {
                <a href="@Url.Action("List", "Topic", new {page = Model.Paging.CurrentPage - 1})"
                   class="btn btn-default @prevDisabled">
                    &lt; 前一页
                </a>
                
                <a href="@Url.Action("List", "Topic", new {page = Model.Paging.CurrentPage + 1})"
                   class="btn btn-default @nextDisabled">
                    后一页 &gt;
                </a>
            }
        </div>

    </div>
</div>


@section Tail{
    <script>
        $(document).ready(function() {
            window.DiscussionApp.utils.transformTimestampOn('.last-replied-at', 'data-last-replied-at');
        });
    </script>
}